Public Class EditFiguresFor

    Private _newFileForFigure As New System.Collections.ObjectModel.Collection(Of String)
    Private _deletedFileForFigure As New System.Collections.ObjectModel.Collection(Of Integer)

    Private _fileId As Integer

    Public Event FiguresForChanged()

    Private Sub EditFiguresFor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.FiguresTableAdapter.Fill(Me.FiguresData.Figures)
        Me.FileForFigureTableAdapter.Fill(Me.FileForFigureData.FileForFigure, _
                                          _fileId)

    End Sub

    Private Sub AddFigureForFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFigureForFile.Click

        Dim newFileForFigure As FileForFigureData.FileForFigureRow = FileForFigureData.FileForFigure.NewFileForFigureRow
        newFileForFigure.figure = FigureList.Text
        newFileForFigure.file_id = _fileId
        Try
            FileForFigureData.FileForFigure.AddFileForFigureRow(newFileForFigure)
            _newFileForFigure.Add(FigureList.Text)
        Catch ex As Exception

        End Try
        FileForFiguresList.Refresh()

    End Sub

    Private Sub RemoveFileFigureFor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveFileFigureFor.Click

        Dim entryRow As FileForFigureData.FileForFigureRow
        Dim rowsToDelete As New System.Collections.ObjectModel.Collection(Of FileForFigureData.FileForFigureRow)

        For Each entry As System.Data.DataRowView In FileForFiguresList.SelectedItems
            entryRow = CType(entry.Row, FileForFigureData.FileForFigureRow)
            If entryRow.RowState = DataRowState.Added Then
                _newFileForFigure.Remove(entryRow.figure)
            Else
                _deletedFileForFigure.Add(entryRow.ff_id)
            End If
            rowsToDelete.Add(entryRow)
        Next

        For Each entryRow In rowsToDelete
            FileForFigureData.FileForFigure.RemoveFileForFigureRow(entryRow)
        Next

        FileForFiguresList.Refresh()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        FileForFigureDS.CancelEdit()
        Me.Close()

    End Sub

    Private Sub UpdateFileDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateFileDetails.Click

        Const ExpectedMessage As String = "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."

        Dim updated As Integer

        For Each deletedFigure As Integer In _deletedFileForFigure
            Me.FileForFigureTableAdapter.Delete(deletedFigure)
        Next

        FileForFigureDS.EndEdit()
        Try

            updated = Me.FileForFigureTableAdapter.Update(Me.FileForFigureData)


        Catch ex As OleDb.OleDbException
            If ex.Message <> ExpectedMessage Then
                MsgBox(ex.Message)
            End If
        End Try

        RaiseEvent FiguresForChanged()

        Me.Close()

    End Sub

    Public Sub New(ByVal fileId As Integer)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _fileId = fileId

    End Sub

End Class